package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.constant.BlogConstant;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TWebNavbarMapper;
import com.have.blog.model.models.TWebNavbar;
import com.have.blog.model.models.TWebNavbarExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.service.admin.AdminWebNavbarService;
import com.have.blog.xo.vo.AdminWebNavbarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminWebNavbarServiceImpl implements AdminWebNavbarService {
    @Autowired
    private TWebNavbarMapper webNavbarMapper;

    @Override
    public String getPageList(AdminWebNavbarVo webNavbarVO) {
        PageHelper.startPage(webNavbarVO.getCurrentPage(), webNavbarVO.getPageSize());
        TWebNavbarExample example = new TWebNavbarExample();
        TWebNavbarExample.Criteria criteria = example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (!StringUtils.isEmpty(webNavbarVO.getKeyword()) && !StringUtils.isEmpty(webNavbarVO.getKeyword().trim())) {
            criteria.andNameLike("%" + webNavbarVO.getKeyword() + "%");
        }
        if (webNavbarVO.getNavbarLevel() != 0) {
            criteria.andNavbarLevelEqualTo(webNavbarVO.getNavbarLevel());
        }
        example.setOrderByClause("sord desc");
        List<TWebNavbar> webNavbarList = webNavbarMapper.selectByExample(example);
        return ResultUtils.successWithData(CommonPage.restPage(webNavbarList));
    }

    @Override
    public String getAllList() {
        TWebNavbarExample example = new TWebNavbarExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andNavbarLevelEqualTo(BlogConstant.NUM_ONE);
        example.setOrderByClause("sort desc");
        List<TWebNavbar> webNavbarList = webNavbarMapper.selectByExample(example);

        // 查询子目录
        List<String> uids = new ArrayList<>();
        webNavbarList.forEach(item -> {
            uids.add(item.getUid());
        });
        example.clear();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andParentUidIn(uids);
        List<TWebNavbar> childList = webNavbarMapper.selectByExample(example);
        for (TWebNavbar parent : webNavbarList) {
            List<TWebNavbar> tempList = new ArrayList<>();
            for (TWebNavbar child : childList) {
                if (parent.getUid().equals(child.getParentUid())) {
                    tempList.add(child);
                }
            }
            Collections.sort(tempList, new Comparator<TWebNavbar>() {
                @Override
                public int compare(TWebNavbar o1, TWebNavbar o2) {
                    return o2.getSort() - o1.getSort();
                }
            });
            parent.setChildWebNavbar(tempList);
        }
        return ResultUtils.successWithData(webNavbarList);
    }

    @Override
    public String addWebNavbar(AdminWebNavbarVo webNavbarVO) {
        if (webNavbarVO.getNavbarLevel() == BlogConstant.NUM_ONE) {
            webNavbarVO.setParentUid(null);
        }
        TWebNavbar tWebNavbar = new TWebNavbar();
        BeanUtils.copyProperties(webNavbarVO, tWebNavbar);
        tWebNavbar.setStatus(BlogStatus.ENABLE.getStatus());
        tWebNavbar.setUid(MyStringUtils.getUUID());
        tWebNavbar.setCreateTime(new Date());
        tWebNavbar.setUpdateTime(new Date());
        int count = webNavbarMapper.insert(tWebNavbar);
        if (count > 0) {
            return ResultUtils.successWithMessage(MessageConstant.INSERT_SUCCESS);
        } else {
            return ResultUtils.errorWithMessage(MessageConstant.INSERT_FAIL);
        }
    }

    @Override
    public String editWebNavbar(AdminWebNavbarVo webNavbarVO) {
        if (webNavbarVO.getNavbarLevel() == BlogConstant.NUM_ONE) {
            webNavbarVO.setParentUid(null);
        }
        TWebNavbar webNavbar = webNavbarMapper.selectByPrimaryKey(webNavbarVO.getUid());
        BeanUtils.copyProperties(webNavbarVO, webNavbar);
        webNavbar.setUpdateTime(new Date());
        int count = webNavbarMapper.updateByPrimaryKeySelective(webNavbar);
        if (count > 0) {
            return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
        } else {
            return ResultUtils.errorWithMessage(MessageConstant.UPDATE_FAIL);
        }
    }

    @Override
    public String deleteWebNavbar(AdminWebNavbarVo webNavbarVO) {
        TWebNavbarExample example = new TWebNavbarExample();
        example.createCriteria()
                .andParentUidEqualTo(webNavbarVO.getUid())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        long num = webNavbarMapper.countByExample(example);
        if (num > 0) {
            return ResultUtils.errorWithMessage(MessageConstant.CHILDREN_MENU_UNDER_THIS_MENU);
        }
        TWebNavbar webNavbar = webNavbarMapper.selectByPrimaryKey(webNavbarVO.getUid());
        webNavbar.setUpdateTime(new Date());
        webNavbar.setStatus(BlogStatus.DISABLE.getStatus());
        int count = webNavbarMapper.updateByPrimaryKeySelective(webNavbar);
        if (count > 0) {
            return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
        }
        return ResultUtils.errorWithMessage(MessageConstant.DELETE_FAIL);
    }
}
